package com.gitee.xmhzzz.component.web.intercepter;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson2.JSONObject;
import com.gitee.xmhzzz.component.web.config.XmhWebProperties;
import com.gitee.xmhzzz.component.web.utils.RequestContextMng;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

/**
  * @ClassName
  * @Description
  * @Author wzq
  * @Date 2023/12/18 9:47
  * @Version 1.0
  */
@ControllerAdvice
@Slf4j
public class AccessLogInterceptor extends RequestBodyAdviceAdapter implements HandlerInterceptor, InterceptorRegisterBean {

    @Autowired
    private XmhWebProperties yipuleWebProperties;

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        if (!yipuleWebProperties.isEnable()) {
            return inputMessage;
        }
        InputStream is = inputMessage.getBody();
        byte[] bytes = IoUtil.readBytes(is);
        printBody(new String(bytes));
        return new HttpInputMessage() {
            @Override
            public InputStream getBody() {
                return new ByteArrayInputStream(bytes);
            }

            @Override
            public HttpHeaders getHeaders() {
                return inputMessage.getHeaders();
            }
        };
    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        return true;
    }

    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }

    private static void printBody(Object body) {
        if (!RequestContextMng.context().inited()) {
            return;
        }
        String url = RequestContextMng.context().getRequest().getRequestURL().toString() + (RequestContextMng.context().getRequest().getQueryString() == null ? "" : "?" + RequestContextMng.context().getRequest().getQueryString());
        if (body == null) {
            log.info("url: {}, 请求体为空", url);
        } else if (body instanceof String) {
            log.info("url: {}, 请求体: {}", url, body);
        } else {
            log.info("url: {}, 请求体: {}", url, JSONObject.toJSONString(body));
        }
    }
}
